package com.admin.service.impl;

import com.admin.entity.SysDept;
import com.admin.entity.SysUser;
import com.admin.mapper.SysUserMapper;
import com.admin.service.DeptService;
import com.admin.service.HomeService;
import com.admin.service.PermissionService;
import com.admin.web.response.HomeResp;
import com.admin.web.response.PermissionNodeResp;
import com.admin.web.response.UserInfoResp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页服务
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private DeptService deptService;
    @Resource
    private PermissionService permissionService;

    @Override
    public HomeResp getHomeInfo(String userId) {


        SysUser sysUser = sysUserMapper.selectById(userId);
        UserInfoResp vo = new UserInfoResp();

        if (sysUser != null) {
            BeanUtils.copyProperties(sysUser, vo);
            SysDept sysDept = deptService.getById(sysUser.getDeptId());
            if (sysDept != null) {
                vo.setDeptId(sysDept.getId());
                vo.setDeptName(sysDept.getName());
            }
        }

        List<PermissionNodeResp> menus = permissionService.permissionTreeList(userId);

        HomeResp respVO = new HomeResp();
        respVO.setMenus(menus);
        respVO.setUserInfo(vo);

        return respVO;
    }
}
